<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/17
 * Time: 下午12:46
 */

namespace App\Library\Mall;


use App\Library\AuthenticatedUser;
use Illuminate\Http\Request;

trait ShopTrait
{
    use AuthenticatedUser;
    /**
     * @param Request $request
     * @return \App\Models\Shop|bool|\Illuminate\Database\Eloquent\Model|mixed
     */
    public function storeShop(Request $request)
    {
        $shop = array_only($request->post('shop'), [
            'shop_name',
            'logo',
            'type',
            'phone',
            'province',
            'city',
            'district',
            'street',
            'description',
            'lat',
            'lng'
        ]);
        $content = $request->post('content');
        if ($shop['shop_name'] && $shop['phone']) {
            if ($myShop = $this->user()->shop) {
                $myShop->update($shop);
            }else {
                $myShop = $this->user()->shop()->create($shop);
            }
            if ($content) $myShop->content()->update(array_only($content, ['content']));
            $this->shopSaved($request, $myShop);
            return $this->sendStoreShopResponse($request, $myShop);
        }

        return $this->sendStoreShopFailedResponse($request);
    }

    protected function shopSaved(Request $request, $shop)
    {

    }

    /**
     * @param Request $request
     * @param $shop
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendStoreShopResponse(Request $request, $shop)
    {
        return ajaxReturn(['shop'=>$shop]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendStoreShopFailedResponse(Request $request)
    {
        return ajaxError(201, 'invalid parameter');
    }

    /**
     * @param Request $request
     * @return \App\Models\ShopAuth|\Illuminate\Database\Eloquent\Model|\Illuminate\Http\JsonResponse|mixed
     */
    public function storeAuth(Request $request)
    {
        $auth = $request->input('auth');
        if ($auth['shop_owner'] && $auth['id_card_no'] && $auth['id_card_pic_1'] &&
            $auth['id_card_pic_2'] && $auth['id_card_pic_3'] && $auth['license_pic'] && $auth['license_no']){

            $auth = array_only($auth, [
                'shop_owner',
                'id_card_no',
                'id_card_pic_1',
                'id_card_pic_2',
                'id_card_pic_3',
                'license_no',
                'license_pic',
                'other_pic',
            ]);
            $auth['auth_status'] = 0;
            $myAuth = $this->user()->shop->auth;
            $myAuth->update($auth);
            return $this->sendStoreAuthResponse($request, $myAuth);
        }
        return $this->sendStoreAuthFailedResponse($request);
    }

    protected function sendStoreAuthResponse(Request $request, $auth)
    {
        return ajaxReturn(['auth'=>$auth]);
    }

    protected function sendStoreAuthFailedResponse(Request $request)
    {
        return ajaxError(201, '');
    }
}
